package com.flick.exception;

import com.flick.common.Result;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;

@ControllerAdvice
@Slf4j
public class GlobalExceptionHandler  {

    /**
     * 全局异常捕捉处理
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = Exception.class)
    public Result errorHandler(Exception ex) {
        Result result = new Result("500",ex.getMessage());
        log.error(ex.getMessage());
        ex.printStackTrace();
        return result;
    }

    /**
     * 拦截捕捉自定义异常 CommonException.class
     * @param ex
     * @return
     */
    @ResponseBody
    @ExceptionHandler(value = CommonException.class)
    public Result myErrorHandler(CommonException ex) {
        Result result = new Result(ex.getCode(),ex.getMessage());
        log.error(ex.getMessage());
        ex.printStackTrace();
        return result;
    }

}
